package com.loveofsoftware.quotes;

import java.util.Date;

import com.loveofsoftware.quotes.R;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.TextView;

public class QuoteADayPageActivity extends Activity {
	
	String [] quotes ;
	String [] authors ;
	int currIndex = 0;
	TextView textview =  null;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        textview = new TextView(this);
        textview.setLayoutParams(lp);
        textview.setTextColor(Color.YELLOW);
        textview.setTextSize(16);//16sp
        textview.setSingleLine(false);
      
        //android:textStyle="bold|italic" 

        textview.setGravity(Gravity.CENTER);
        
        //android:layout_gravity="center"/>
        
        
        
        textview.setText("");
        setContentView(textview);
        
        Resources r = getResources();
		quotes = r.getStringArray(R.array.quotesArray);
		authors = r.getStringArray(R.array.authorsArray);
		if(quotes.length != authors.length)
		{
			System.out.println("ERROR ============= quotes and authors do not match "+quotes.length+","+authors.length);
		}
		setText();
		currIndex = (new Date()).getDay()%quotes.length;
		textview.setText(quotes[currIndex]+"\n\n"+authors[currIndex]);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            currIndex =(currIndex+1)%quotes.length;
            setText();
        }
        return true;
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;

	}
	
	private void setText()
	{
		//Do nothing
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.next:
			 currIndex =(currIndex+1)%quotes.length;
			 setText();
			return true;
		case R.id.favourite:
			if(0 == currIndex)
				currIndex = quotes.length -1;
			else
				currIndex --;
			setText();
			return true;
		case R.id.exit:
			this.finish();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
}